package com.detrum.detrumdx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.detrum.detrumdx.R;
import com.detrum.wifidevice.bean.WifiInfoBean;

import java.util.List;

/**
 * Created by abc on 2017/11/30.
 */

public class WifiInfoAdapter extends BaseAdapter {
    private Context context;
    private List<WifiInfoBean.DataBean> beanList;
    private LayoutInflater layoutInflater;

    public WifiInfoAdapter(Context context, List<WifiInfoBean.DataBean> beanList) {
        this.context = context;
        this.beanList = beanList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return beanList==null?0:beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position).getSSID();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WifiHolder holder;
        if(convertView==null){
            convertView = layoutInflater.inflate(R.layout.wifi_item,parent,false);
            holder = new WifiHolder();
            holder.SSID = convertView.findViewById(R.id.wifi_name);
            holder.isConnect = convertView.findViewById(R.id.connect);
            holder.RSSI = convertView.findViewById(R.id.wifi_rssi);
            convertView.setTag(holder);
        }else{
            holder = (WifiHolder) convertView.getTag();
        }
        holder.SSID.setText(beanList.get(position).getSSID());
        if(beanList.get(position).isConnect()){
            holder.isConnect.setText(R.string.connected);
            holder.RSSI.setVisibility(View.GONE);
        }else{
            int rssi = beanList.get(position).getRssi();
            if(rssi==0){
                holder.isConnect.setText(R.string.no_sign);
                holder.RSSI.setVisibility(View.GONE);
            }else if(rssi>0&&rssi<=30){
                holder.isConnect.setVisibility(View.GONE);
                holder.RSSI.setImageResource(R.mipmap.ic_rssi_1);
            }else if(rssi>30&&rssi<=70){
                holder.isConnect.setVisibility(View.GONE);
                holder.RSSI.setImageResource(R.mipmap.ic_rssi_2);
            }else{
                holder.isConnect.setVisibility(View.GONE);
                holder.RSSI.setImageResource(R.mipmap.ic_rssi);
            }
        }

        return convertView;
    }

    class WifiHolder{
        TextView SSID;
        TextView isConnect;
        ImageView RSSI;
    }
}
